/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'dart:ui';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:hadss_adaptive_layout/hadss_adaptive_layout.dart';

class InnerBanner extends StatefulWidget {
  const InnerBanner(
      {Key? key, required this.currentWidth, required this.screenType})
      : super(key: key);
  final num currentWidth;
  final WidthBreakpoint screenType;

  @override
  State<InnerBanner> createState() => _BannerState();
}

class _BannerState extends State<InnerBanner> {
  @override
  Widget build(BuildContext context) {
    return Container(
        width: widget.currentWidth * 1.0,
        margin: const EdgeInsets.only(top: 10),
        child: getBannerWiddet(widget.screenType));
  }

  Widget getBannerWiddet(screenType) {
    List mdImages = [
      'lib/assets/images/banner/md-banner-2.png',
      'lib/assets/images/banner/md-banner-1.png',
      'lib/assets/images/banner/md-banner-3.png',
      'lib/assets/images/banner/md-banner-4.png'
    ];

    List lgImages = [
      'lib/assets/images/banner/p-banner-1.png',
      'lib/assets/images/banner/p-banner-2.png',
      'lib/assets/images/banner/p-banner-3.png',
      'lib/assets/images/banner/p-banner-4.png'
    ];

    if (screenType == WidthBreakpoint.sm || screenType == WidthBreakpoint.xs) {
      return const BannerForSm();
    } else if (screenType == WidthBreakpoint.md) {
      return ScrollConfiguration(
          behavior: ScrollConfiguration.of(context).copyWith(
            dragDevices: {
              PointerDeviceKind.touch,
              PointerDeviceKind.mouse,
              PointerDeviceKind.trackpad,
            },
          ),
          child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                  children: mdImages.map((e) {
                return Container(
                    width: 310,
                    height: 170,
                    margin: const EdgeInsets.only(right: 10, left: 10),
                    child: Image.asset(e));
              }).toList())));
    }
    return ScrollConfiguration(
        behavior: ScrollConfiguration.of(context).copyWith(
          dragDevices: {
            PointerDeviceKind.touch,
            PointerDeviceKind.mouse,
            PointerDeviceKind.trackpad,
          },
        ),
        child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
                children: lgImages.map((e) {
              return Container(
                  width: 510,
                  height: 220,
                  margin: const EdgeInsets.only(right: 10, left: 12),
                  child:
                      Image.asset(e));
            }).toList())));
  }
}

class BannerForSm extends StatefulWidget {
  const BannerForSm({Key? key}) : super(key: key);

  @override
  State<BannerForSm> createState() => _BannerForSmState();
}

class _BannerForSmState extends State<BannerForSm> {
  List<String> imageURls = [
    'lib/assets/images/banner/sm-banner.png',
    'lib/assets/images/banner/md-banner-2.png',
    'lib/assets/images/banner/md-banner-3.png',
  ];

  int _current = 0;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width,
      child: ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: Stack(children: [
            SizedBox(
              width: MediaQuery.of(context).size.width-10 ,
              height: (MediaQuery.of(context).size.width-28)* 219 / 328,
              child: ScrollConfiguration(
                behavior: ScrollConfiguration.of(context).copyWith(
                  dragDevices: {
                    PointerDeviceKind.touch,
                    PointerDeviceKind.mouse,
                    PointerDeviceKind.trackpad,
                  },
                ),
                child:Swiper(
                itemBuilder: (context, index) {
                  final image = imageURls[index];
                  return Container(
                    margin: const EdgeInsets.only(left: 16,right: 8),
                    child: Image.asset(
                    image,
                    fit: BoxFit.fill,
                  ),);
                },
                indicatorLayout: PageIndicatorLayout.COLOR,
                autoplay: false,

                itemCount: imageURls.length,
                onIndexChanged: (currentIndex) {
                  setState(() {
                    _current = currentIndex;
                  });
                },
              )),
            ),
            Positioned(
                bottom: 10,
                child: SizedBox(
                  width: MediaQuery.of(context).size.width,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: imageURls.asMap().entries.map((entry) {
                      return GestureDetector(
                        onTap: () {
                        },
                        child: Container(
                          width: _current == entry.key ? 12 : 6.0,
                          height: 6.0,
                          margin: const EdgeInsets.symmetric(
                              vertical: 8.0, horizontal: 4.0),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            color: _current == entry.key
                                ? Colors.white
                                : Colors.grey,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                )),
          ])),
    );
  }
}
